﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Extra.ERP;

namespace Dictionary
{
    public class xucHolidayAdd : xucBaseAddH
    {
        public delegate void SuccessEventHander(object sender, DIC_HOLIDAY item);

        private readonly IContainer components = null;
        private DateEdit dtFromDate;
        private DateEdit dtToDate;
        private GroupControl groupControl1;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private Guid m_HolidayID = Guid.Empty;
        private TextEdit txtDescription;
        private TextEdit txtName;

        public xucHolidayAdd()
        {
            InitializeComponent();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            txtName.Focus();
            dtFromDate.DateTime = DateTime.Now;
            dtToDate.DateTime = DateTime.Now;
            base.Add();
        }

        public void Clear()
        {
            txtName.Text = "";
            txtDescription.Text = "";
            dtFromDate.DateTime = DateTime.Now;
            dtToDate.DateTime = DateTime.Now;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucHolidayAdd));
            groupControl1 = new GroupControl();
            dtToDate = new DateEdit();
            labelControl4 = new LabelControl();
            dtFromDate = new DateEdit();
            labelControl3 = new LabelControl();
            txtDescription = new TextEdit();
            labelControl2 = new LabelControl();
            txtName = new TextEdit();
            labelControl1 = new LabelControl();
            ((ISupportInitialize) base.Err).BeginInit();
            base.imageCollection2.BeginInit();
            groupControl1.BeginInit();
            groupControl1.SuspendLayout();
            dtToDate.Properties.VistaTimeProperties.BeginInit();
            dtToDate.Properties.BeginInit();
            dtFromDate.Properties.VistaTimeProperties.BeginInit();
            dtFromDate.Properties.BeginInit();
            txtDescription.Properties.BeginInit();
            txtName.Properties.BeginInit();
            base.SuspendLayout();
            base.btnSave.Location = new Point(0x47, 0xb0);
            base.btnSave.TabIndex = 1;
            base.btnCancel.Location = new Point(270, 0xb0);
            base.btnCancel.TabIndex = 3;
            base.btnSaveNew.Location = new Point(0xa9, 0xb0);
            base.btnSaveNew.TabIndex = 2;
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            groupControl1.Controls.Add(dtToDate);
            groupControl1.Controls.Add(labelControl4);
            groupControl1.Controls.Add(dtFromDate);
            groupControl1.Controls.Add(labelControl3);
            groupControl1.Controls.Add(txtDescription);
            groupControl1.Controls.Add(labelControl2);
            groupControl1.Controls.Add(txtName);
            groupControl1.Controls.Add(labelControl1);
            groupControl1.Dock = DockStyle.Top;
            groupControl1.Location = new Point(0, 0);
            groupControl1.Name = "groupControl1";
            groupControl1.Size = new Size(0x179, 0xa4);
            groupControl1.TabIndex = 0;
            groupControl1.Text = "Th\x00f4ng tin ng\x00e0y nghỉ, ng\x00e0y lể";
            dtToDate.EditValue = null;
            dtToDate.Location = new Point(0xfd, 0x47);
            dtToDate.Name = "dtToDate";
            dtToDate.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            dtToDate.Properties.VistaTimeProperties.Buttons.AddRange(new[] {new EditorButton()});
            dtToDate.Size = new Size(0x70, 20);
            dtToDate.TabIndex = 5;
            labelControl4.Location = new Point(0xc6, 0x4a);
            labelControl4.Name = "labelControl4";
            labelControl4.Size = new Size(0x33, 13);
            labelControl4.TabIndex = 4;
            labelControl4.Text = "Đến ng\x00e0y:";
            dtFromDate.EditValue = null;
            dtFromDate.Location = new Point(0x4f, 0x47);
            dtFromDate.Name = "dtFromDate";
            dtFromDate.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            dtFromDate.Properties.VistaTimeProperties.Buttons.AddRange(new[] {new EditorButton()});
            dtFromDate.Size = new Size(0x71, 20);
            dtFromDate.TabIndex = 3;
            labelControl3.Location = new Point(12, 0x48);
            labelControl3.Name = "labelControl3";
            labelControl3.Size = new Size(0x2c, 13);
            labelControl3.TabIndex = 2;
            labelControl3.Text = "Từ ng\x00e0y:";
            txtDescription.Location = new Point(0x4f, 0x68);
            txtDescription.Name = "txtDescription";
            txtDescription.Properties.AutoHeight = false;
            txtDescription.Size = new Size(0x11e, 50);
            txtDescription.TabIndex = 7;
            labelControl2.Location = new Point(13, 0x68);
            labelControl2.Name = "labelControl2";
            labelControl2.Size = new Size(0x27, 13);
            labelControl2.TabIndex = 6;
            labelControl2.Text = "Ghi ch\x00fa:";
            txtName.Location = new Point(0x4f, 0x26);
            txtName.Name = "txtName";
            txtName.Size = new Size(0x11e, 20);
            txtName.TabIndex = 1;
            labelControl1.Location = new Point(12, 0x27);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(60, 13);
            labelControl1.TabIndex = 0;
            labelControl1.Text = "T\x00ean ng\x00e0y lể:";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(groupControl1);
            base.Name = "xucHolidayAdd";
            base.Size = new Size(0x179, 0xd7);
            base.Controls.SetChildIndex(base.btnSaveNew, 0);
            base.Controls.SetChildIndex(base.btnSave, 0);
            base.Controls.SetChildIndex(base.btnCancel, 0);
            base.Controls.SetChildIndex(groupControl1, 0);
            ((ISupportInitialize) base.Err).EndInit();
            base.imageCollection2.EndInit();
            groupControl1.EndInit();
            groupControl1.ResumeLayout(false);
            groupControl1.PerformLayout();
            dtToDate.Properties.VistaTimeProperties.EndInit();
            dtToDate.Properties.EndInit();
            dtFromDate.Properties.VistaTimeProperties.EndInit();
            dtFromDate.Properties.EndInit();
            txtDescription.Properties.EndInit();
            txtName.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_HOLIDAY item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_HOLIDAY item)
        {
            m_HolidayID = item.HolidayID;
            SYS_LOG.Insert("Danh Mục Ng\x00e0y Nghỉ", "Xem", m_HolidayID.ToString());
            txtName.Text = item.HolidayName;
            dtFromDate.DateTime = item.FromDate;
            dtToDate.DateTime = item.ToDate;
            txtDescription.Text = item.Description;
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_HOLIDAY
            {
                HolidayID = m_HolidayID
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            SYS_LOG.Insert("Danh Mục Ng\x00e0y Nghỉ", "Th\x00eam", m_HolidayID.ToString());
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            m_HolidayID = Guid.NewGuid();
            var item = new DIC_HOLIDAY(m_HolidayID, txtName.Text, dtFromDate.DateTime, dtToDate.DateTime,
                txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            SYS_LOG.Insert("Danh Mục Ng\x00e0y Nghỉ", "Cập Nhật", m_HolidayID.ToString());
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_HOLIDAY(m_HolidayID, txtName.Text, dtFromDate.DateTime, dtToDate.DateTime,
                txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}